<?php

class SiteController extends Controller {
	/**
	 * @var array filters
	 */
	public $filters;
	
	/**
	 * Declares class-based actions.
	 */
	public function actions() {
		return array(
			// captcha action renders the CAPTCHA image displayed on the contact page
			'captcha' => array(
				'class' => 'CCaptchaAction',
				'backColor' => 0xFFFFFF
			),
			// page action renders "static" pages stored under 'protected/views/site/pages'
			// They can be accessed via: index.php?r=site/page&view=FileName
			'page' => array(
				'class' => 'CViewAction'
			)
		);
	}
	
	/**
	 * This is the default 'index' action that is invoked
	 * when an action is not explicitly requested by users.
	 */
	public function actionIndex() {
		$c = new MyDbCriteria();
		$c->compare('visibility', Post::VISIBILITY_PUBLIC);
		
		$this->initFilters($c);
		foreach ($this->filters as $f) {
			$f->apply();
		}
		
		$dataProvider = new CActiveDataProvider('Post', array('criteria' => $c));
		$dataProvider->pagination->pageSize = $c->limit;
		$c->limit = null;
		
		$this->render('index', array(
			'dataProvider' => $dataProvider
		));
	}
	
	public function initFilters($criteria) {
		$this->filters = array(
			'category' => new CategoryFilter(),
			'status' => new StatusFilter(),
			'tag' => new TagFilter(),
			'rating' => new RatingFilter(),
			'search' => new SearchForm(),
			'toolbar' => new Toolbar()
		);
		
		foreach ($this->filters as $f)
			$f->init($criteria);
	}
	
	public function renderBlock($name) {
		$block = isset($this->filters[$name]) ? $this->filters[$name] : null;
		if (!$block)
			return;
		$this->renderPartial($block->getTemplate(), array(
			'block' => $block
		));
	}
	
	public function getCurrentUrl() {
		return $this->createUrl('index', $_GET);
	}
	
	/**
	 * This is the action to handle external exceptions.
	 */
	public function actionError() {
		$this->layout = '/layouts/1col';
		if ($error = Yii::app()->errorHandler->error) {
			if (Yii::app()->request->isAjaxRequest)
				echo $error['message'];
			else
				$this->render('error', $error);
		}
	}
	
	/**
	 * This is the action to handle external exceptions.
	 */
	public function actionLogin() {
		$this->redirect(array('admin/login'));
	}

	/**
	 * Displays the contact page
	 */
	public function actionContact() {
		$model = new ContactForm;
		if (isset($_POST['ContactForm'])) {
			$model->attributes = $_POST['ContactForm'];
			if ($model->validate()) {
				$name    = '=?UTF-8?B?' . base64_encode($model->name) . '?=';
				$subject = '=?UTF-8?B?' . base64_encode($model->subject) . '?=';
				$headers = "From: $name <{$model->email}>\r\n" . "Reply-To: {$model->email}\r\n" . "MIME-Version: 1.0\r\n" . "Content-type: text/plain; charset=UTF-8";
				
				mail(Yii::app()->params['adminEmail'], $subject, $model->body, $headers);
				Yii::app()->user->setFlash('contact', 'Thank you for contacting us. We will respond to you as soon as possible.');
				$this->refresh();
			}
		}
		$this->render('contact', array(
			'model' => $model
		));
	}
	
	/**
	 * Display post edit dialog
	 */
	public function actionEditPost($id) {
		$model = Post::model()->findByPk($id);
		$model->scenario = 'quick_edit';
		
		if (isset($_POST['Post'])) {
			$model->attributes = $_POST['Post'];
			if ($model->save()) {
				Yii::app()->user->setFlash('success', 'The post "'.$model->title.'" has been saved.');
				$returnUrl = isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('index');
				$this->redirect($returnUrl);
			}
		}
		$this->renderPartial('//catalog/post/edit', array(
			'model' => $model
		));
	}
}